package com.des1gn.ymgetil.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.des1gn.ymgetil.enums.ApiEnums;
import com.des1gn.ymgetil.model.MiaoProduct;
import com.des1gn.ymgetil.model.MiaoProductDetal;
import com.des1gn.ymgetil.service.MiaoService;
import com.des1gn.ymgetil.service.ProxyService;
import com.des1gn.ymgetil.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.Proxy;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName MiaoServiceImpl
 * @Description TODO
 * @Author Des1gn
 * @Date 2022/1/11 11:31
 * @Version 1.0
 **/
@Service
public class MiaoServiceImpl implements MiaoService {


    @Autowired
    ProxyService proxyService;

    @Override
    public List<MiaoProduct> getMiaoProducts() {
        List<MiaoProduct> result = new LinkedList<>();
        JSONObject okHttpMiao = HttpUtil.getOkHttpMiao(ApiEnums.GET_MIAOPRODUCTLIST.getApiUrl(), null, null);
        if (okHttpMiao.getInteger("code")==0000){
            JSONArray data = okHttpMiao.getJSONArray("data");
            if (data != null && data.size() > 0){
                for (Object obj : data){
                   MiaoProduct miaoProduct = JSONObject.parseObject(JSONObject.toJSONString(obj),MiaoProduct.class);
                   result.add(miaoProduct);
                }
            }
        }
        return result;
    }

    @Override
    public MiaoProductDetal getMiaoProductDetail(String sessionId,String id) {
        MiaoProductDetal miaoProductDetal = new MiaoProductDetal();
        String url = ApiEnums.GET_MIAOPRODUCTDETAIL.getApiUrl().replaceAll("ID",id);
        JSONObject okHttpMiao = HttpUtil.getOkHttpMiao(url, sessionId, null);
        if (okHttpMiao.getInteger("code")==0000){
            miaoProductDetal = JSONObject.parseObject(JSONObject.toJSONString(okHttpMiao.getJSONObject("data")),MiaoProductDetal.class);
        }
        return miaoProductDetal;
    }

    @Override
    public String submitOrder(String sessionId, String msId, String userId, String idCardNo) {
        Proxy randomProxy = proxyService.getRandomProxy();
        String apiUrl = ApiEnums.GET_SUBMITORDER.getApiUrl();
        apiUrl = apiUrl.replaceAll("MSID",msId);
        apiUrl = apiUrl.replaceAll("USERID",userId);
        apiUrl = apiUrl.replaceAll("IDCARDNO",idCardNo);
        MD5 md5 = MD5.create();
        String sign = md5.digestHex(md5.digestHex(msId.concat(userId).concat(Long.toString(System.currentTimeMillis()/1000L))).concat("ux$ad70*b"));
        JSONObject okHttpMiaoSubmit = HttpUtil.getOkHttpMiaoSubmit(apiUrl, sessionId, sign, randomProxy);
        if (null != okHttpMiaoSubmit && okHttpMiaoSubmit.containsKey("code") &&  okHttpMiaoSubmit.getInteger("code")==0000){
            return okHttpMiaoSubmit.getString("data");
        }
        return "";
    }

    @Override
    public List<String> getDates(String sessionId, String msId, String orderId) {
        List<String> dates = new LinkedList<>();
        Proxy randomProxy = proxyService.getRandomProxy();
        String apiUrl = ApiEnums.GET_DATE.getApiUrl();
        apiUrl = apiUrl.replaceAll("MSID",msId);
        apiUrl = apiUrl.replaceAll("ORDERID",orderId);
        JSONObject okHttpMiao = HttpUtil.getOkHttpMiao(apiUrl, sessionId, randomProxy);
        if (null != okHttpMiao && okHttpMiao.containsKey("code") &&  okHttpMiao.getInteger("code")==0000){
            JSONArray data = okHttpMiao.getJSONArray("data");
            if (null != data && data.size() > 0){
                data.forEach(i->{
                    dates.add(String.valueOf(i));
                });
            }
        }
        return dates;
    }

    @Override
    public List<String> getTimes(String sessionId, String msId, String orderId, String day) {
        List<String> times = new LinkedList<>();
        Proxy randomProxy = proxyService.getRandomProxy();
        String apiUrl = ApiEnums.GET_TIMES.getApiUrl();
        apiUrl = apiUrl.replaceAll("MSID",msId);
        apiUrl = apiUrl.replaceAll("ORDERID",orderId);
        apiUrl = apiUrl.replaceAll("DAY",day);
        JSONObject okHttpMiao = HttpUtil.getOkHttpMiao(apiUrl, sessionId, randomProxy);
        if (null != okHttpMiao && okHttpMiao.containsKey("code") &&  okHttpMiao.getInteger("code")==0000){
            JSONArray data = okHttpMiao.getJSONArray("data");
            if (null != data && data.size() > 0){
                data.forEach(i->{
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(i));
                    times.add(jsonObject.getString("wid"));
                });
            }
        }
        return times;
    }

    @Override
    public Boolean toSubmit(String sessionId, String msId, String orderId, String day, String wid) {
        Proxy randomProxy = proxyService.getRandomProxy();
        String apiUrl = ApiEnums.GET_TO.getApiUrl();
        apiUrl = apiUrl.replaceAll("MSID",msId);
        apiUrl = apiUrl.replaceAll("ORDERID",orderId);
        apiUrl = apiUrl.replaceAll("DAY",day);
        apiUrl = apiUrl.replaceAll("WID",wid);
        JSONObject okHttpMiao = HttpUtil.getOkHttpMiao(apiUrl, sessionId, randomProxy);
        if (null != okHttpMiao && okHttpMiao.containsKey("code") &&  okHttpMiao.getInteger("code")==0000){
            return true;
        }
        return false;
    }


}
